package environment;

import org.newdawn.slick.*;

public class Guy implements Actor {
	SpriteSheet guySheet = null;
	static final int SLIM_DOWN = 11;
	static final int SHORTEN = 5;
	Image[] rightImages;
	Animation rightAnim = null;
	Image[] leftImages;
	Animation leftAnim = null;
	Image guyUp = null;
	Image guyDown = null;
	int guyDir = 0; // 0 = down, 1 = right, 2 = up, 3 = left
	private Mobile m;
	private Dirt dirt;
	
	int stepTimer = 0;
	int stepInterval = 10;
	int hSpeed = 4;
	int jumpPower = 12;

	public Guy(GameContainer gc, Dirt dirt, int x, int y) throws SlickException {
		// Load Images
		guySheet = new SpriteSheet("res/diggerguy.png", 33, 36, 0);
		guyDown = guySheet.getSprite(0, 0);
		rightImages = new Image[]{guySheet.getSprite(2, 1), guySheet.getSprite(1, 1), guySheet.getSprite(0, 1)};
		rightAnim = new Animation(rightImages, 200);
		guyUp = guySheet.getSprite(0, 2);
		leftImages = new Image[]{guySheet.getSprite(0, 3), guySheet.getSprite(1, 3), guySheet.getSprite(2, 3)};
		leftAnim = new Animation(leftImages, 200);
		m = new Mobile(gc, dirt, x + SLIM_DOWN, y + SHORTEN, Dirt.GRID_SCALE - 2 * SLIM_DOWN, Dirt.GRID_SCALE - SHORTEN);
		this.dirt = dirt;
	}
	
	public void render(GameContainer gc, Graphics g) {
		switch(guyDir) {
		case 0:
			guyDown.draw(m.getX() - SLIM_DOWN, m.getY() - SHORTEN);
			break;
		case 1:
			rightAnim.draw(m.getX() - SLIM_DOWN, m.getY() - SHORTEN);
			break;
		case 2:
			guyUp.draw(m.getX() - SLIM_DOWN, m.getY() - SHORTEN);
			break;
		case 3:
			leftAnim.draw(m.getX() - SLIM_DOWN, m.getY() - SHORTEN);
			break;
		}
	}

	public void update(GameContainer gc, int timePassed) throws SlickException {
		dirt.debugLines.clear();
		//Controls
		Input input = gc.getInput();
		
		float timeR = timePassed / Dirt.INTERVAL;
		float hGo = hSpeed * timeR;
		
		leftAnim.stop();
		rightAnim.stop();
		
		if(input.isKeyDown(Input.KEY_RIGHT) && !input.isKeyDown(Input.KEY_LEFT)) {
			rightAnim.start();
			m.move(hGo, 0f);
			guyDir = 1;
			stepTimer = (stepTimer + 1) % stepInterval;
			if(stepTimer == 0) dirt.sndStp.play();
		}
		
		if(input.isKeyDown(Input.KEY_LEFT) && !input.isKeyDown(Input.KEY_RIGHT)) {
			leftAnim.start();
			m.move(-hGo, 0f);
			guyDir = 3;
			stepTimer = (stepTimer + 1) % stepInterval;
			if(stepTimer == 0) dirt.sndStp.play();
		}
		
		if(input.isKeyDown(Input.KEY_DOWN)) {
			if (m.getVVelocity() == 0f) {
				int gridX = (int)((m.getX() + m.getRight()) * .5f) / Dirt.GRID_SCALE;
				int gridY = (m.getBottom() + 1) / Dirt.GRID_SCALE;
				dirt.destroy(gridX, gridY);
			}
			guyDir = 0;
		}
		
		if(input.isKeyDown(Input.KEY_UP)) {
			if (m.getVVelocity() == 0f) {
				int gridX = m.getX() / Dirt.GRID_SCALE;
				int gridX2 = m.getRight() / Dirt.GRID_SCALE;
				int gridY = (m.getBottom() + 1) / Dirt.GRID_SCALE;
				if (!dirt.isMovable(gridX, gridY) || !dirt.isMovable(gridX2, gridY)) {
					m.addVVelocity(-jumpPower);
					dirt.sndJmp.play();
				}
			}
		}

		m.update(gc, timePassed, dirt);
		
		// Test for collision with bomb
		int gridX = m.getX() / Dirt.GRID_SCALE;
		int gridX2 = m.getRight() / Dirt.GRID_SCALE;
		int gridY = m.getBottom() / Dirt.GRID_SCALE;
		if(dirt.hasBomb(gridX, gridY) || dirt.hasBomb(gridX2, gridY)) {
			System.out.println("BAAAABOOOOOOM!!!!! You blew up.  Score: " + m.getY());
			dirt.sndBmb.play();
			while(dirt.sndBmb.playing());
			System.exit(0);
		}
	}

	public int getX() {
		return m.getX();
	}

	public int getY() {
		return m.getY();
	}
}
